package com.dhj.mall.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dhj.mall.bean.*;
import com.dhj.mall.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AnalysisService {

	@Autowired
	MyorderMapper myorderMapper;

	@Autowired
	Me_addressMapper me_addressMapper;

	@Autowired
	Or_goodMapper or_goodMapper;

	@Autowired
	GoodMapper goodMapper;

	@Autowired
	MemberMapper memberMapper;

	public Map<String, Integer> getRegionalAnalysis(String pro, String area) {

		String city = pro + " " + area;

		int jdtype = 0, jjtype = 0, nzMtype = 0, sjtype = 0, nytype = 0, nzWtype = 0, sptype = 0, xxtype = 0,
				xbtype = 0, bhtype = 0;

		List<Myorder> myorders = myorderMapper.selectByExample(null);
		for (Myorder myorder : myorders) {
			Me_address me_address = me_addressMapper.selectByPrimaryKey(myorder.getOrAddressid());
			if (me_address.getAdDistrict().equals(city)) {

				Or_goodExample myorder_example = new Or_goodExample();
				com.dhj.mall.bean.Or_goodExample.Criteria myorder_criteria = myorder_example.createCriteria();
				myorder_criteria.andGoOridEqualTo(myorder.getOrId());
				List<Or_good> or_goodlist = or_goodMapper.selectByExample(myorder_example);

				Good g = goodMapper.selectByPrimaryKey(or_goodlist.get(0).getGoGoodid());
				if (g.getGoType().equals("家电")) {
					jdtype++;
				} else if (g.getGoType().equals("家居家纺")) {
					jjtype++;
				} else if (g.getGoType().equals("男装")) {
					nzMtype++;
				} else if (g.getGoType().equals("手机数码")) {
					sjtype++;
				} else if (g.getGoType().equals("内衣配饰")) {
					nytype++;
				} else if (g.getGoType().equals("女装")) {
					nzWtype++;
				} else if (g.getGoType().equals("食品")) {
					sptype++;
				} else if (g.getGoType().equals("鞋靴")) {
					xxtype++;
				} else if (g.getGoType().equals("箱包")) {
					xbtype++;
				} else {
					bhtype++;
				}

			}
		}

		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("jdtype", jdtype);
		map.put("jjtype", jjtype);
		map.put("nzMtype", nzMtype);
		map.put("sjtype", sjtype);
		map.put("nytype", nytype);
		map.put("nzWtype", nzWtype);
		map.put("sptype", sptype);
		map.put("xxtype", xxtype);
		map.put("xbtype", xbtype);
		map.put("bhtype", bhtype);
		return map;
	}

	public Map<String, List<Integer>> getGradeAnalysis() {
		int jvip1 = 0;
		int jvip2 = 0;
		int jvip3 = 0;
		int jvip4 = 0;
		int jvip5 = 0;
		int jvip6 = 0;
		int bvip1 = 0;
		int bvip2 = 0;
		int bvip3 = 0;
		int bvip4 = 0;
		int bvip5 = 0;
		int bvip6 = 0;
		List<Myorder> myorders = myorderMapper.selectByExample(null);
		List<Integer> viList = new ArrayList<Integer>();
		List<Integer> vbList = new ArrayList<Integer>();
		for (Myorder myorder : myorders) {

			if (myorder.getOrExpendval() != null) {
				// 1、积分订单
				int levelid = memberMapper.selectByPrimaryKey(myorder.getOrMemberid()).getMeLevelid();
				switch (levelid) {
				case 1:
					jvip1++;
					break;
				case 2:
					jvip2++;
					break;
				case 3:
					jvip3++;
					break;
				case 4:
					jvip4++;
					break;
				case 5:
					jvip5++;
					break;
				case 6:
					jvip6++;
					break;
				}
			} else {
				// 2、普通订单
				int levelid = memberMapper.selectByPrimaryKey(myorder.getOrMemberid()).getMeLevelid();
				switch (levelid) {
				case 1:
					bvip1++;
					break;
				case 2:
					bvip2++;
					break;
				case 3:
					bvip3++;
					break;
				case 4:
					bvip4++;
					break;
				case 5:
					bvip5++;
					break;
				case 6:
					bvip6++;
					break;
				}
			}

		}
		Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
		viList.add(jvip1);viList.add(jvip2);viList.add(jvip3);
		viList.add(jvip4);viList.add(jvip5);viList.add(jvip6);
		
		vbList.add(bvip1);vbList.add(bvip2);vbList.add(bvip3);
		vbList.add(bvip4);vbList.add(bvip5);vbList.add(bvip6);
		
		map.put("integral", viList);map.put("general", vbList);
		return map;
	}

	public List<Integer> getGroupAnalysiss(String date) throws ParseException {
		List<Integer> list = new ArrayList<Integer>();
		
		MyorderExample example = new MyorderExample();
		com.dhj.mall.bean.MyorderExample.Criteria criteria = example.createCriteria();
		criteria.andOrDateLike(date+"%");
		List<Myorder> myorders = myorderMapper.selectByExample(example);
		
		int mSum = 0;
		int WSum = 0;
		int sum10_17 = 0;
		int sum18_35 = 0;
		int sum36_59 = 0;
		int sum60up = 0;
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		for (Myorder myorder : myorders) {
			Member member = memberMapper.selectByPrimaryKey(myorder.getOrMemberid());
			Date brDate = df.parse(member.getMeBrithday());
			if (member.getMeSex().equals("M")) {
				mSum++;
			}else if (member.getMeSex().equals("F")) {
				WSum++;
			}
			
			if (getMouth(brDate)>=120&&getMouth(brDate)<194) {
				sum10_17++;
			}else if (getMouth(brDate)>=194&&getMouth(brDate)<420) {
				sum18_35++;
			}else if (getMouth(brDate)>=420&&getMouth(brDate)<708) {
				sum36_59++;
			}else {
				sum60up++;
			}
		}
		
		list.add(mSum);
		list.add(WSum);
		list.add(sum10_17);
		list.add(sum18_35);
		list.add(sum36_59);
		list.add(sum60up);
		return list;
	}

	private int getMouth(Date brDate) {
		Date now = new Date();
		Date date3 = new Date(now.getTime() - brDate.getTime());
		int mouth = (int) Math.floor(date3.getTime()/(30 * 24L * 60L * 60L * 1000L));
		return mouth;
	}

}
